# @FileName  : main.py
# @Time      : 2025/2/6 7:18
# @Author    : LuZhaoHui
# @Software  : PyCharm


# Please install OpenAI SDK first: `pip3 install openai`

from tool import *
from openai import OpenAI
import requests
import json
import base64

# DeepSeek API 的端点
apiUrl = "https://api.deepseek.com/v1/chat/completions"

apiModel = "deepseek-chat"

# 你的 API 密钥
apiKey = "sk-e5f03225fe684d038ccff9205629adde"

# 请求头
headers = {
    "Authorization": f"Bearer {apiKey}",
    "Content-Type": "application/json"
}


# 对话请求的 payload
def initPay(model=''):
    return {"model": model if len(model) else apiModel,
            "messages": [],
            "attachments": []
            }


def addPayMessage(pay, message):
    pay["messages"].append({"role": "user",
                            "content": message})
    return pay


def addPayAttachments(pay, file_name, isBin=False):
    with open(file_name, "rb") as file:
        if isBin:
            file_content = base64.b64encode(file.read()).decode("utf-8")
        else:
            file_content = file.read()
    pay["attachments"].append({"file_name": file_name,
                               "file_content": file_content})
    return pay


# payload = {
#     "model": "deepseek-v3",
#     "messages": [
#         {"role": "user",
#          "content": "你好"}
#     ],
#     "attachments": [
#         # {
#         #     "file_name": "example.txt",
#         #     "file_content": "这是附件的内容。"
#         # }
#     ]
# }


# 如果有附件，可以在这里添加
# attachment_path = "path_to_your_attachment_file"
# with open(attachment_path, "rb") as file:
#     attachment_data = file.read()

# 将附件添加到 payload 中
# payload["attachments"].append({
#     "filename": "attachment.txt",  # 文件名
#     "content_type": "text/plain",  # 文件类型
#     "data": attachment_data.decode("utf-8")  # 文件内容
# })

# 发送请求
# response = requests.post(api_url, headers=headers, data=json.dumps(payload))

# 处理响应
# if response.status_code == 200:
#     response_data = response.json()
#     print("DeepSeek 的回复:", response_data.get("response"))
# else:
#     print("请求失败，状态码:", response.status_code)
#     print("错误信息:", response.text)

def test2(reqText):
    # 发送请求
    pay = initPay()
    pay = addPayMessage(pay, reqText)
    response = requests.post(apiUrl, headers=headers, data=json.dumps(pay))

    # 处理响应
    if response.status_code == 200:
        result = response.json()
        print("API 调用成功！")
        print("响应内容:", result)
    else:
        print(f"API 调用失败，状态码: {response.status_code}")
        print("错误信息:", response.text)


def test1(reqText):
    # 第一个对话
    client = OpenAI(api_key=apiKey, base_url="https://api.deepseek.com")

    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=[
            {"role": "system", "content": "你是我的朋友"},
            {"role": "user", "content": reqText},
        ],
        stream=False
    )
    print(response.choices[0].message.content)
    # saveLog(response.choices[0].message.content, log='api.txt')


if __name__ == '__main__':
    print('deepSeek test')
    test1("hello")
    # test1("你好,我是小白,想知道如何学习deepseek的API调用")
